<template>
  <div>
    <el-card class="card">
      <div class="ordertitle">
        <h4>我最近的订单</h4>
      </div>
      <!-- <div  style="width:100%;background-color:rgb(240,240,240);height:2px;"></div> -->
      <div class="title">
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
          <el-tab-pane label="全部订单" name="first"></el-tab-pane>
          <el-tab-pane label="未支付" name="second"></el-tab-pane>
          <el-tab-pane label="已付款" name="third"></el-tab-pane>
          <el-tab-pane label="已发货" name="fourth"></el-tab-pane>
          <el-tab-pane label="已收货" name="fifth"></el-tab-pane>
          <el-tab-pane label="已取消" name="sixth"></el-tab-pane>
        </el-tabs>
        <el-form :inline="true">
          <el-form-item label="订单时间:">
            <el-select v-model="time">
              <el-option value="0" label="全部"></el-option>
              <el-option value="1" label="近三个月"></el-option>
              <el-option value="2" label="近六个月"></el-option>
              <el-option value="3" label="近一年"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="inquiry" >搜索</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div v-if="haveData">
        <el-table :data="orderData" @expand-change="expand">
          <el-table-column type="expand" width="55">
            <template slot-scope="scope">
              <div>
                <span style="float: left">{{ scope.row.shippingUser }}</span
                ><span style="margin-left: 10%; margin-right: 10%">{{
                  scope.row.receiverMobile
                }}</span
                ><span>{{ scope.row.address }}</span>
              </div>
              <el-table :data="detailData">
                <el-table-column type="index"></el-table-column>
                <el-table-column prop="product" label="产品信息">
                  <template slot-scope="scope">
                    <div style="display: flex">
                      <img
                        :src="scope.row.mainImage"
                        height="60px"
                        width="60px"
                        @click="toProduct(scope.row)" style="margin-right:20px"
                      />
                      <div >
                        <div @click="toProduct(scope.row)" style="float: left;width:300px">
                          {{ scope.row.productName }}
                        </div>
                        <div style="float: right">
                          <span style="margin-left:100px; margin-right: 20px;"
                            >x {{ scope.row.productCnt }}</span
                          ><span>￥{{ scope.row.productPrice }}</span>
                        </div>
                      </div>
                    </div>
                  </template>
                </el-table-column>
              </el-table>
            </template>
          </el-table-column>
          <!-- <el-table-column type="selection" width="55px"></el-table-column> -->
          <el-table-column prop="id" label="订单号"></el-table-column>
          <el-table-column prop="orderMoney" label="金额(元)"></el-table-column>
          <el-table-column
            prop="orderStatus.name"
            label="订单状态"
          ></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <div v-if="scope.row.orderStatus.code === 0">
                <el-button type="primary" size="mini" @click="pay(scope.row)"
                  >支付</el-button
                >
                <el-button
                  type="danger"
                  size="mini"
                  @click="cancelOrder(scope.row)"
                  >取消</el-button
                >
              </div>
              <div v-else-if="scope.row.orderStatus.code === 1">
                <el-button type="danger" size="mini" @click="changeOrder(scope.row)">退款</el-button>
              </div>
              <div v-else-if="scope.row.orderStatus.code === 2">
                <el-button
                  type="danger"
                  size="mini"
                  @click="deleteOrder(scope.index, scope.row, orderData)"
                  >删除</el-button
                >
              </div>
              <div v-else-if="scope.row.orderStatus.code === 3">
                <el-button
                  type="primary"
                  size="mini"
                  @click="receiveOrder(scope.row)"
                  >签收</el-button
                >
                <el-button type="info" size="mini" @click="checkShip()"
                  >查看物流</el-button
                >
                <el-button
                  type="danger"
                  size="mini"
                  @click="changeOrder(scope.row)"
                  >退货</el-button
                >
              </div>
              <div v-else-if="scope.row.orderStatus.code === 4">
                <el-button
                  type="primary"
                  size="mini"
                  @click="commentOrder(scope.row)"
                  >评价</el-button
                >
                <el-button
                  type="danger"
                  size="mini"
                  @click="deleteOrder(scope.index, scope.row, orderData)"
                  >删除</el-button
                >
              </div>
              <div v-else>
                <el-button
                  type="danger"
                  size="mini"
                  @click="deleteOrder(scope.index, scope.row, orderData)"
                  >删除</el-button
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="keyword.currentPage"
          :page-size="keyword.pageSize"
          layout="total, prev, pager, next"
          :total="total"
        ></el-pagination>
      </div>

      <div v-else>
        <img
          src="@/assets/images/no.png"
          style="margin-left: 30%; margin-top: 50px; margin-bottom: 10px"
        />
        <div>
          您最近没有购买任何商品，先去<el-link
            style="color: salmon; margin-top: -5px; font-size: 16px"
            href="/"
          >
            挑选商品 </el-link
          >吧~
        </div>
      </div>
      <el-dialog :title="title" :visible="visible" :before-close="handleClose">
          <div v-if="title==='支付'">
            <div>
              <span style="color: pink; font-size: 20px; margin: 5px">确定前往支付页面</span>
              <span slot="footer" class="dialog-footer">
                <el-button @click="visible = false">再想想</el-button>
                <el-button type="primary" @click="jumpZ">去支付</el-button>
              </span>
            </div>
          </div>
          <div v-else>
            <Ship></Ship>
          </div>
        </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { selectByMemberId, selectDetailsByOrderId,updateOrder } from "@/api/order.js";
import { selectByIdA } from "@/api/address.js";
import ShareData from "@/utils/localStorage.js";
import Ship from "@/views/member/order/Ship.vue"
export default {
  components:{Ship},
  data() {
    return {
      title:"支付",
      visible:false,
      haveData: false,
      orderData: [],
      detailData: [],
      activeName: "first",
      time: "",
      keyword: { pageSize: 10, currentPage: 1,day:null },
      head: "https://zpl-1303223223.cos.ap-beijing.myqcloud.com",
      total: 1,
      order:{},
      oId:"",
    };
  },
  mounted() {
    this.showOrder();
  },
  methods: {
    handleClick(tab) {
      console.log(tab.index);
      this.showOrder()
    },
    expand(row) {
      selectByIdA(row.addressId).then((res) => {
        // console.log(res)
        if (res.data.code === "0000") {
          row.receiverMobile = res.data.data.receiverMobile;
          row.address =
            res.data.data.receiverProvince +
            res.data.data.receiverCity +
            res.data.data.receiverDistrict +
            res.data.data.receiverZip;
        }
      });
      selectDetailsByOrderId(row.id).then((res) => {
        console.log(res);
        if (res.data.code === "0000") {
          this.detailData = res.data.data;
          for (let i = 0; i < res.data.data.length; i++) {
            this.detailData[i].mainImage =
              this.head + res.data.data[i].mainImage;
          }
        }
      });
    },
    showOrder() {
      
      this.orderData = []
      let memberId = ShareData.getItem("memberId");
      this.keyword.memberId = memberId;
      this.keyword.day = null;
      console.log(this.activeName)
      if (this.activeName === "first") {
        this.keyword.orderStatus = null;
      }else if(this.activeName === "second"){
        this.keyword.orderStatus = 0;
      }else if(this.activeName === "third"){
        this.keyword.orderStatus = 1;
      }else if(this.activeName === "fourth"){
        this.keyword.orderStatus = 3;
      }else if(this.activeName === "fifth"){
        this.keyword.orderStatus = 4;
      }else if(this.activeName === "sixth"){
        this.keyword.orderStatus = 2;
      }
        selectByMemberId(this.keyword).then((res) => {
          if (res.data.code === "0000") {
            console.log(res.data.data)
            if (res.data.data.records.length !== 0) {
              this.haveData = true;
              this.orderData = res.data.data.records;
              this.total = res.data.data.total;
            }else{
              this.haveData = false;
            }
          }
        });
      },
    toProduct(row) {
      this.$router.push("/detail?id=" + row.productId);
    },
     handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },
    inquiry(){
      this.keyword.orderStatus = null;
      this.activeName="first"
      this.orderData = []
      if(this.time==="0"){
        this.keyword.day=null
      }else if(this.time==="1"){
        this.keyword.day=90
      }else if(this.time==="2"){
        this.keyword.day=180
      }else if(this.time==="3"){
        this.keyword.day=365
      }

      selectByMemberId(this.keyword).then((res) => {
          if (res.data.code === "0000") {
            console.log(res)
            if (res.data.data.records.length !== 0) {
              this.haveData = true;
              this.orderData = res.data.data.records;
              this.total = res.data.data.total;
            }else{
              this.haveData = false;
            }
          }
        });

    },
    pay(row){
      this.visible=true
      this.title="支付"
      this.oId=row.id;
      // console.log(row)
    },
    jumpZ(){
      window.location.href="http://localhost:8008/order-service/order/payByAli/"+this.oId;
    },
    cancelOrder(row){
    if(confirm("确定要取消订单吗？")){
      this.order=row
      this.order.paymentMethod=row.paymentMethod.code
      this.order.orderStatus=2
      updateOrder(this.order).then(res=>{
        if(res.data.code==="0000"){
          // this.$message.success("取消成功")
          this.showOrder()
        }
      })
    }
},
deleteOrder(index,row,orderData){
   if(confirm("确定要删除订单吗？")){
     this.order=row
     this.order.orderStatus=row.orderStatus.code
     this.order.paymentMethod=row.paymentMethod.code
      this.order.deleteStatus=1
      this.total--
      updateOrder(this.order).then(res=>{
        if(res.data.code==="0000"){
          orderData.splice(index,1)
        }
      })
    }

},
receiveOrder(row){
  if(confirm("确定要签收订单吗？")){
      this.order.id=row.id
      this.order.paymentMethod=row.paymentMethod.code
      this.order.orderStatus=4
      updateOrder( this.order).then(res=>{
        if(res.data.code==="0000"){
           this.showOrder()
        }
      })
    }
},
checkShip(){
    this.title="物流信息"
    this.visible=true
},
changeOrder(row){
  if(confirm("确定要退货吗？")){
      this.order=row
      this.order.paymentMethod=row.paymentMethod.code
      this.order.orderStatus=5
      updateOrder(this.order).then(res=>{
        if(res.data.code==="0000"){
           this.showOrder()
        }
      })
    }
},
handleClose(){
this.visible=false
},
commentOrder(){
this.$message.success("本服务暂未开放")
}
    
  },
};
</script>

<style lang="scss" scoped>
.card {
  padding: 3%;
  margin-bottom: 14%;
}
.ordertitle {
  text-align: left;
  margin-top: -20px;
}
.ordertitle .el-link {
  margin-top: -30px;
  float: right;
  color: silver;
}
.title .el-form {
  float: right;
  margin-top: -60px;
}
.el-select {
  width: 120px;
}
.el-option {
  width: 130px;
}
.el-table {
  padding-left: 2%;
  padding-right: 2%;
}
</style>